Repository navigation
Conversation
|
@QinXi-ai Can you isolate the benchmark creation in its own commit and rebase it as the first commit so that we can run benchmark like #27435 (comment) |
c624a1f to
024788c
Compare
|
Thanks @kosiew. I split the PR into two commits. You can run the grouped COUNT(DISTINCT) suite at a9807de for the baseline, then at 024788c for the inline-first accumulator. The final tree matches the previous PR head. One observation from a separate local experiment: Q10 and Q11 have dense integer group keys and at most two distinct values per group. A prototype that exploits both properties took Q10 from 381 to 4.9 ms and Q11 from 407 to 4.9 ms on a Xeon, relative to the benchmark-only commit. That was with one SQL partition and eight workers inside the prototype. With eight SQL partitions, both queries were slightly slower than baseline. I’ve kept the prototype on a separate branch. |
|
run benchmark grouped_count_distinct baseline:
ref: a9807de48a
env:
DATAFUSION_OPTIMIZER_SINGLE_DISTINCT_PREFER_NATIVE: "true"
DATAFUSION_EXECUTION_TARGET_PARTITIONS: "1"
MEM_POOL_TYPE: greedy
MEMORY_LIMIT: 1G |
|
Benchmark for this request failed before finishing (Kubernetes reason: Benchmarks requested: Runner log (last 40 lines)Kubernetes messageFile an issue against this benchmark runner |
024788c to
3afc0b8
Compare
|
run benchmark grouped_count_distinct baseline:
ref: 312ca330b6d76ef93b2cbfec3fd1b6f48cea174f
env:
DATAFUSION_OPTIMIZER_SINGLE_DISTINCT_PREFER_NATIVE: "true"
DATAFUSION_EXECUTION_TARGET_PARTITIONS: "1"
MEM_POOL_TYPE: greedy
MEMORY_LIMIT: 1G |
|
Hi @QinXi-ai, thanks for the request (#27299 (comment)). Only whitelisted users can trigger benchmarks. Allowed users: 2010YOUY01, Dandandan, Fokko, Jefffrey, Omega359, Rachelint, Rich-T-kid, adriangb, alamb, andygrove, asubiotto, avantgardnerio, brunal, buraksenn, cetra3, codephage2020, coderfender, comphead, erenavsarogullari, etseidl, friendlymatthew, gabotechs, geoffreyclaude, grtlr, gruuya, haohuaijin, jayzhan211, jonathanc-n, kevinjqliu, klion26, kosiew, kumarUjjawal, kunalsinghdadhwal, liamzwbao, mbutrovich, mkleen, mzabaluev, neilconway, rluvaton, sdf-jkl, timsaucer, xudong963, zhuqi-lucas. File an issue against this benchmark runner |
|
@kosiew I rebased both commits onto Could you rerun this benchmark command with the updated baseline? The runner requires an allowlisted account. |
|
run benchmark grouped_count_distinct baseline:
ref: 312ca330b6d76ef93b2cbfec3fd1b6f48cea174f
env:
DATAFUSION_OPTIMIZER_SINGLE_DISTINCT_PREFER_NATIVE: "true"
DATAFUSION_EXECUTION_TARGET_PARTITIONS: "1"
MEM_POOL_TYPE: greedy
MEMORY_LIMIT: 1G |
|
🤖 Benchmark running (GKE) | trigger CPU Details (lscpu)Comparing perf/25936-inline-first-distinct-20261110 (3afc0b8) to 312ca33 diff Run configurationrun benchmark grouped_count_distinct
env:
DATAFUSION_EXECUTION_TARGET_PARTITIONS: "1"
DATAFUSION_OPTIMIZER_SINGLE_DISTINCT_PREFER_NATIVE: "true"
MEMORY_LIMIT: "1G"
MEM_POOL_TYPE: "greedy"
baseline:
ref: "312ca330b6d76ef93b2cbfec3fd1b6f48cea174f"Trigger commentResults will be posted here when complete File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Instance: Comparing perf/25936-inline-first-distinct-20261110 (3afc0b8) to 312ca33 diff Run configurationrun benchmark grouped_count_distinct
env:
DATAFUSION_EXECUTION_TARGET_PARTITIONS: "1"
DATAFUSION_OPTIMIZER_SINGLE_DISTINCT_PREFER_NATIVE: "true"
MEMORY_LIMIT: "1G"
MEM_POOL_TYPE: "greedy"
baseline:
ref: "312ca330b6d76ef93b2cbfec3fd1b6f48cea174f"Trigger commentCPU Details (lscpu)Details
Memory Pool PeaksPeak Base:
Pool accounting vs. process RSS Max pool peak is the largest reservation any single query in the run reached; peak RSS covers the whole invocation, including data loading and allocator retention, and the two high-water marks need not coincide in time. The gap is therefore an upper bound on what the pool did not account for, not a measurement of it.
Resource Usagegrouped_count_distinct — base (merge-base)
grouped_count_distinct — branch
File an issue against this benchmark runner |
There was a problem hiding this comment.
Thanks for the optimization. The inline fast path preserves distinct-count semantics and partial-state compatibility while substantially improving singleton performance and memory usage.
Benchmark results look great.
Approving with suggestions. The increased memory usage for very sparse inputs is a bounded trade-off worth documenting or addressing separately.
| } | ||
| }); | ||
| if let Some(first_values) = &mut self.first_values { | ||
| first_values.resize(total_num_groups, T::Native::default()); |
There was a problem hiding this comment.
Could we consider density-based promotion in update_batch and merge_batch? With twelve sparse DISTINCT expressions, peak reservations increased from 5.24 MB to 8.38 MB, introducing spills at 6M. Any guard should preserve Q10/Q11 gains; otherwise, documenting the trade-off is sufficient.
There was a problem hiding this comment.
i am not sure worriying about a few MB is worth extra complexity at this point
| fn evaluate(&mut self, emit_to: EmitTo) -> datafusion_common::Result<ArrayRef> { | ||
| let counts = emit_to.take_needed(&mut self.counts); | ||
| if let Some(first_values) = &mut self.first_values { | ||
| let _ = emit_to.take_needed(first_values); |
There was a problem hiding this comment.
Optional: consider dropping drain(..n) directly for small discarded inline prefixes to avoid allocation. Retain split-and-drop for large prefixes so the remaining buffer can shrink.
| first value, which lets repeated singleton values skip the distinct hash set. | ||
| The rewrite's outer aggregate only sees the deduplicated pairs. | ||
|
|
||
| When the average number of additional distinct values per group exceeds eight, |
There was a problem hiding this comment.
Could we clarify that promotion is checked after each update or merge batch, includes NULL-only groups in the average, and remains active through partial emissions until EmitTo::All resets the accumulator?
3afc0b8 to
643f16b
Compare
alamb
left a comment
There was a problem hiding this comment.
Than you for trying this @QinXi-ai
I think this PR makes the code significantly more complicated and it looks like a special case to me of a synthetic benchmark (and I am not sure exactly how it works). Unless there is super compelling use case / actual query you are runing I don't think we should make this change
| first value, which lets repeated singleton values skip the distinct hash set. | ||
| The rewrite's outer aggregate only sees the deduplicated pairs. | ||
|
|
||
| After each update or merge batch, the native accumulator checks the average |
There was a problem hiding this comment.
this described how the accumulator works, not the benchmark -- it should be near the accumualtro
|
Thanks, @alamb. I can see why Q10 alone was a hard sell for the added complexity. I replayed a published PostgreSQL query unchanged in DataFusion on the public Instacart dataset. It counts the distinct aisles and departments in each grocery order, then averages those counts across 3.35 million orders. Original SQL What helped me understand the result was that 91% of the baskets contained items from multiple departments. Keeping the first department inline saves one hash-set insertion per basket, even when later items introduce other departments. The aisle count benefits in the same way. With the default DISTINCT optimizer settings, median time for the full query fell from 3.44 to 2.93 seconds at 4 partitions and from 2.15 to 1.80 seconds at 8. Peak memory reserved by the pool fell 14–17%. Both measures improved in every paired round, and both builds reproduced the published results. That gives me a concrete reason to keep the first-value optimization. I’d be happy to revisit the logic that switches back to hashing if we can make it easier to follow. |
Thanks @QinXi-ai -- I agree it is a clever observation. Thank you for the link back to the original query I am still struggling to see the big picture here. It seems like maybe this PR is an attempt to improve the performance the |
|
run benchmark clickbench_partitioned |
1 similar comment
|
run benchmark clickbench_partitioned |
|
🤖 Benchmark running (GKE) | trigger CPU Details (lscpu)Comparing perf/25936-inline-first-distinct-20261110 (643f16b) to 4277853 (merge-base) diff Run configurationrun benchmark clickbench_partitionedTrigger commentResults will be posted here when complete File an issue against this benchmark runner |
|
🤖 Benchmark running (GKE) | trigger CPU Details (lscpu)Comparing perf/25936-inline-first-distinct-20261110 (643f16b) to 4277853 (merge-base) diff Run configurationrun benchmark clickbench_partitionedTrigger commentResults will be posted here when complete File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Instance: Comparing perf/25936-inline-first-distinct-20261110 (643f16b) to 4277853 (merge-base) diff Run configurationrun benchmark clickbench_partitionedTrigger commentCPU Details (lscpu)Details
Resource Usageclickbench_partitioned — base (merge-base)
clickbench_partitioned — branch
File an issue against this benchmark runner |
|
🤖 Benchmark completed (GKE) | trigger Instance: Comparing perf/25936-inline-first-distinct-20261110 (643f16b) to 4277853 (merge-base) diff Run configurationrun benchmark clickbench_partitionedTrigger commentCPU Details (lscpu)Details
Resource Usageclickbench_partitioned — base (merge-base)
clickbench_partitioned — branch
File an issue against this benchmark runner |
|
Thank you for taking another look, @alamb — and for running Yes — the broader goal is to make DataFusion's native grouped
This PR stays self-contained inside the existing integer accumulator and does not commit us to any planner changes yet. I also have the README cleanup ready locally (moving the accumulator docs into |
Which issue does this PR close?
Part of #25936. This implements the accumulator step in the follow-up plan.
Rationale for this change
The native grouped integer
COUNT(DISTINCT)accumulator currently probes the shared distinct hash set for every non-null input row. For Q10, which has a million groups with four copies of one value per group, keeping that value inline avoids those probes.I built both versions from
c8265b68on a shared Linux host, using a Xeon Gold 6238T, Rust 1.99.1 andrelease-nonlto. The suite used four million rows, one target partition, greedy / 1 GiB, withsingle_distinct_prefer_nativeenabled. These are medians from three alternating rounds, six iterations per round with the first discarded, giving 15 retained samples per version. CPU affinity was 0-7, with builds stopped during timing.The inline buffer allocates a typed slot for every group, including groups with only NULL arguments. Q12 exposes that cost, with about 6.7 MiB more peak pool memory.
What changes are included in this PR?
Keep each group's first non-null value in a typed vector and put additional values in the shared hash set. After a batch where the average number of additional distinct values per group exceeds eight, move the inline values into the set and release the vector. Subsequent batches use the hash-only loop. This avoids the per-row cache check for the large sets in Q07 and Q09. CPU-0 repeats measured -2.9% and -3.5% elapsed changes for those two queries.
Both modes use the existing List state format. State emission, partial emission, merge and memory accounting handle the inline values and the switch to the hash-only mode.
Add Q11 and Q12 to the SQL benchmark suite to cover small distinct sets and NULL-heavy groups.
What is the testing strategy for this PR?
Added unit tests for partial emission, overlapping merges, group-index shifts, promotion, buffer accounting and reuse. SQL tests exercise small batches and overlapping partial states through the normal query path.
Validated on Linux with Rust 1.99.1.
uv run ./dev/rust_lint.shsuite passed.Are there any user-facing changes?
Native grouped integer
COUNT(DISTINCT)has the runtime and memory changes shown above.Prepared with Codex assistance. Opening as a draft for author review.